package competition.dao.impl;

import competition.BaseDao;
import competition.dao.VersionDao;
import competition.po.TVersionEntity;
import competition.vo.AreaName;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Order;
import org.springframework.stereotype.Repository;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Created by Administrator on 2017/10/30/030.
 */
@Repository(value = "versionDao")
public class VersionDaoImpl extends BaseDao implements VersionDao {
    public boolean insertVersion(String name, String apk) {
        Session session=getCurrentSession();
        Transaction transaction=session.beginTransaction();
        TVersionEntity tVersionEntity=new TVersionEntity();

        tVersionEntity.setName(name);
        tVersionEntity.setApk(apk);
        Date dNow = new Date();
        SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        tVersionEntity.setUploadTime(ft.format(dNow).toString());
        try {
            session.save(tVersionEntity);
            transaction.commit();
            return true;
        }catch (Exception e){
            transaction.rollback();
        }
        return false;
    }

    public List returnVersion() {
        Session session=getCurrentSession();
        Transaction transaction=session.beginTransaction();
        Order order=Order.desc("uploadTime");
        Criteria criteria=session.createCriteria(TVersionEntity.class).addOrder(order);
        List<TVersionEntity>list=criteria.list();

        return list;
    }
}
